Home:ALL Converter>Docker swarm: how to place app and db together?

Docker swarm: how to place app and db together?

Ask Time:2018-11-19T16:50:12         Author:kyberorg

Json Formatter

I have stack with 2 services: Spring boot application and mongo database. I want to deploy this stack to Docker Swarm (1 node in Germany, 1 in Finland and 1 in Estonia).

Currently Swarm schedules application to Germany cluster and Database to Finland, which means that every request goes from Germany to Finland.

Is this way how to force Swarm place all pieces of stack to single node ?

P.S. sticking to hostname is not a solution, because if node dies service is down.

My Stack.yml is:

version: '3.3'
services:
app:
  image: kyberorg/boot-mongo
  networks:
  - net
  ports:
    - "8080:8080"
  depends_on:
    - mongo
labels:
  - ee.yadev.bootmongoapp
deploy:
  mode: replicated
  replicas: 1
  update_config:
    parallelism: 1
    delay: 10s
 mongo:
   image: mongo
  networks:
    - net
  volumes:
    - example-mongo:/data/db
  deploy:
    mode: replicated
    replicas: 1
    update_config:
      parallelism: 1
      delay: 10s
networks:
  net:
   driver: overlay
volumes:
  example-mongo:
    external: true

Author:kyberorg,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53371100/docker-swarm-how-to-place-app-and-db-together
Dmytro Sirant :

I also spent half a day looking for the solution, and I found it.\n\nTake a look onto Affinity option, which is pretty well described here: https://blog.docker.com/2015/02/scaling-docker-with-swarm/\n\nAffinity\nIn some cases, the placement of a container must be relative to other containers. Swarm lets you define those relationships through affinities.\n\nThe following will run two Redis servers, while guaranteeing they don’t get scheduled on the same machine: \n\ndocker run -d --name redis_1 -e ‘affinity:container!=redis_’ redis\ndocker run -d --name redis_2 -e ‘affinity:container!=redis_’ redis\n\nAffinities are automatically generated when the relationship between containers is implied. Containers that use options such as –link, –volumes-from and –net=container: get co-scheduled on the same host.",
2019-08-15T19:18:28
giabar :

my first idea is to suggest you to use \"placement constraints\" and \"labels\":\n\nhttps://docs.docker.com/engine/swarm/services/#placement-constraints\nhttps://docs.docker.com/engine/swarm/manage-nodes/#add-or-remove-label-metadata\n\nThanks to them you can assign labels to nodes for each country and then modify your stack in order to force the apps to run on the same node.\n\ndocker node update --label-add country=Germany node-germany\ndocker node update --label-add country=Finland node-finland\n\n--constraint node.labels.region==east\n\nversion: '3.3'\nservices:\n app:\n image: kyberorg/boot-mongo\n deploy:\n placement:\n constraints:\n - country == Germany\n\nmongo:\n image: mongo\n deploy:\n placement:\n constraints:\n - country == Germany\n\nOther useful links:\n\nhttps://semaphoreci.com/community/tutorials/scheduling-services-on-a-docker-swarm-mode-cluster\nhttps://container-solutions.com/using-binpack-with-docker-swarm/\n\nI hope this can help you!",
2018-11-19T09:36:27
vimal-k :

What you are looking for is something like POD in kubernetes where co-location of containers is possible. AFAIK, this is currently not available on docker.\n\nYou can have a look at this repo and see if it is useful: https://github.com/rycus86/podlike\n\nIt's an attempt to bring the concept of pods to docker.\n\nedit: On a side note, if the app and db are so tightly coupled, it makes sense that both are part of the same container.",
2018-11-20T18:08:51
yy